<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class Purchaseby extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $purchase_code = $this->request->post('purchase_code');
            if(!empty($purchase_code)){
                $where['purchase_code'] = ["like", '%'.$purchase_code.'%'];
            }
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['goods_code'] = ["like", '%'.$goods_code.'%'];
            }
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['purchase_code|goods_code'] = ["like", '%'.$keyword.'%'];
            }
            $list_type = $this->request->post('list_type');
            $list_type = $list_type==1?$list_type:2;

            $lists = [];
            $list = Db::name('kuyou_goods_sales_order_info')->where($where)->select();
            foreach ($list as $list_k=>$list_v){
                $row = [];
                $goods = Db::name('kuyou_goods')->where(['goods_code'=>$list_v['goods_code']])->find();
                $row['goods_id'] = $goods['id'];
                $row['goods_code'] = $goods['goods_code'];
                $row['goods_name'] = $goods['goods_code'].'_'.$goods['goods_name'];
                $row['goods_image'] = $goods['goods_image'];
                $row['number'] = $list_v['number'];

                $row['attr_id'] = '';
                $row['attr_name'] = '';
                if($goods['auxiliary']==1){
                    //商品设置属性
                    $row['attr_id'] = $list_v['goods_attr'];
                    $attribute_id = Db::name('kuyou_goods_attr')->where(['id'=>$list_v['goods_attr']])->value('attribute_id');
                    $attribute_ids = Db::name('kuyou_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids');
                    if($attribute_ids){
                        $attribute_ids = explode('&',$attribute_ids);
                        $attribute = Db::name('kuyou_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name');
                        $attribute_name = implode('/',$attribute);
                        $row['attr_name'] = $attribute_name;
                    }
                }

                //商品单位
                $unit = Db::name('kuyou_unit')->where(['id'=>$list_v['goods_unit']])->field('id,name')->find();
                $row['goods_unit'] = $list_v['goods_unit'];
                $row['unit_name'] = isset($unit['name'])?$unit['name']:'';

                //仓库
                $stores = Db::name('kuyou_stores')->where(['id'=>$list_v['stores_id']])->field('id,name')->find();
                $row['stores_id'] = $list_v['stores_id'];
                $row['stores_name'] = isset($stores['name'])?$stores['name']:'';

                //客户
                $customers = Db::name('kuyou_customers')->where(['id'=>$list_v['customers_id']])->field('id,customer_name,customer_code')->find();
                $row['customers_id'] = $list_v['customers_id'];
                $row['customers_name'] = isset($customers['customer_name'])?$customers['customer_name']:'';
                $row['customer_code'] = isset($customers['customer_code'])?$customers['customer_code']:'';

                $row['purchase_code'] = $list_v['purchase_code'];
                $row['purchase_time'] = date('Y-m-d',$list_v['purchase_time']);

                $row['sales_code'] = '';
                $row['sales_time'] = '';
                $row['sales_number'] = '';
                $row['sure_number'] = '';
                $row['sales_purchase_code'] = '';
                $row['warehouse'] = '';
                $row['buy_number'] = '';
                $goods_sales_info = Db::name('kuyou_goods_sales_info')->where(['purchase_order_id'=>$list_v['purchase_code']])->find();
                if($goods_sales_info){
                    if($goods_sales_info['examine_status']==1){
                        $row['sales_code'] = $goods_sales_info['purchase_code'];
                        $row['sales_time'] = date('Y-m-d',$list_v['purchase_time']);
                        $sales_number = $goods_sales_info['number'];
                        $row['sales_number'] = $sales_number;
                        $warehouse = $list_v['number']-$goods_sales_info['number'];
                        $row['warehouse'] = $warehouse;
                        $row['buy_number'] = $warehouse;
                    }else{
                        $warehouse = $list_v['number'];
                        $row['warehouse'] = $warehouse;
                        $row['buy_number'] = $warehouse;
                    }
                }

                $order = Db::name('kuyou_goods_purchase_order_info')->where(['sales_order_id'=>$list_v['purchase_code']])->find();
                if($order){
                    $examine_status = Db::name('kuyou_goods_purchase_order')->where(['purchase_code'=>$order['purchase_code']])->value('examine_status');
                    if($examine_status==1){
                        $row['sales_time'] = date('Y-m-d',$order['purchase_time']);
                        $row['sales_purchase_code'] = $order['purchase_code'];
                        $row['sales_number'] = (float)$order['number'];
                        $row['buy_number'] = (float)$row['buy_number']-(float)$order['number'];
                    }
                }

                $row['now_buy_number'] = '';
                $row['buy_price'] = $goods['purchase'];
                if($list_type==1){
                    if($row['buy_number']!==0){
                        $lists[] = $row;
                    }
                }else{
                    $lists[] = $row;
                }
            }

            $result = array("count" => sizeof($lists), "list" => $lists);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
